<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: admincp_members.php 31986 2012-10-30 05:18:16Z chenmengshu $
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
echo <<<HTML
	<font color='red'>
HTML;
if(getgpc('modify') == 1){
	if(!getgpc('uidarray')){
		echo lang('plugin/group_manager', 'uid_not_null');
	}else if(!getgpc('newgroupid')){
		echo lang('plugin/group_manager', 'groupid_not_null');
	}
}
echo <<<HTML
	</font>
HTML;
$sql = "SELECT * FROM ".DB::table("common_member")." WHERE 1=1";
if(!empty($groups)){
	$sql.=" AND groupid IN(".dimplode($groups).")";
}
$num = count(DB::fetch_all($sql));

$curpage = max(1, $_G['page']);
$page_size = 10;
$start_limit = ($curpage - 1) * $page_size;
$end_limit = $start_limit + $page_size;
$result = DB::fetch_all($sql." LIMIT ".$start_limit.", ".$end_limit);
$multipage = multi($num, $page_size, $curpage, ADMINSCRIPT."?action=plugins&identifier=group_manager&pmod=search&".serialize_para('groupid[]', $groups));
	$usergroups = array();
foreach(C::t('common_usergroup')->range() as $group) {
	switch($group['type']) {
		case 'system': $group['grouptitle'] = '<b>'.$group['grouptitle'].'</b>'; break;
		case 'special': $group['grouptitle'] = '<i>'.$group['grouptitle'].'</i>'; break;
	}
	$usergroups[$group['groupid']] = $group;
}
foreach($result as $member){
	$members .= showtablerow('', array('class="td25"', '', 'title=""'), array(
				"<input type=\"checkbox\" name=\"uidarray[]\" value=\"$member[uid]\"".($member['adminid'] == 1 ? 'disabled' : '')." class=\"checkbox\">",
				"<a href=\"home.php?mod=space&uid=$member[uid]\" target=\"_blank\">$member[username]</a>",
				$member['credits'],
				$usergroups[$member['adminid']]['grouptitle'],
				$usergroups[$member['groupid']]['grouptitle'],
			), TRUE);
}
showformheader("plugins&identifier=group_manager&pmod=search&modify=1&".serialize_para('groupid[]', $groups));
showtableheader(cplang('members_search_result', array('membernum' => $num)));
showsubtitle(array('', 'username', 'credits', 'admingroup', 'usergroup', ''));
echo $members;

$groupselect = array();
$usergroupid = isset($_GET['usergroupid']) && is_array($_GET['usergroupid']) ? $_GET['usergroupid'] : array();
$query = C::t('common_usergroup')->fetch_all_not(array(6, 7), true);
foreach($query as $group) {
	$group['type'] = $group['type'] == 'special' && $group['radminid'] ? 'specialadmin' : $group['type'];
	$groupselect[$group['type']] .= "<option value=\"$group[groupid]\" ".(in_array($group['groupid'], $usergroupid) ? 'selected' : '').">$group[grouptitle]</option>\n";
}
$groupselect = '<optgroup label="'.$lang['usergroups_member'].'">'.$groupselect['member'].'</optgroup>'.
	($groupselect['special'] ? '<optgroup label="'.$lang['usergroups_special'].'">'.$groupselect['special'].'</optgroup>' : '').
	($groupselect['specialadmin'] ? '<optgroup label="'.$lang['usergroups_specialadmin'].'">'.$groupselect['specialadmin'].'</optgroup>' : '').
	'<optgroup label="'.$lang['usergroups_system'].'">'.$groupselect['system'].'</optgroup>';
showtableheader();
showsetting(lang('plugin/group_manager', 'modify_group'), '', '', '<select name="newgroupid"><option value="">------------</option>'.$groupselect.'</select>');
showtablefooter();
showtableheader();
showtablefooter();
showsubmit('submit', 'modify', '<input type="checkbox" name="chkall" onclick="checkAll(\'prefix\', this.form, \'uidarray\');if(this.checked){$(\'allinput\').style.display=\'\';}else{$(\'allcheckbox\').checked = false;$(\'allinput\').style.display=\'none\';}" class="checkbox">'.cplang('select_all'), '&nbsp;&nbsp;&nbsp;<span id="allinput" style="display:none"><input id="allcheckbox" type="checkbox" name="allcheckbox" class="checkbox">'.lang('plugin/group_manager', 'all_selectd').'</span>', $multipage);
showtablefooter();
showformfooter();

$newgroupid  = $_GET['newgroupid'];
//echo serialize($groups);
//include template('group_manager:users');
$condition = array();

$select_all =  $_GET['allcheckbox'];
if(empty($select_all)){
	$uids = array();
	foreach($_GET['uidarray'] as $v){
		$uids[] = intval($v);
	}
}else{
	$uids = searchmembers(array("groupid" => $groups));
}
//echo count($uids);
if(!empty($newgroupid)){
	echo $update_sql;
	$update_sql  = "UPDATE ".DB::table('common_member')."  SET groupid = '".$newgroupid."' WHERE uid IN (".dimplode($uids).")";
	$update_count = DB::query($update_sql);
	 
	if($update_count > 0){
		//cpmsg('groups_setting_succeed', 'action=plugins&identifier=group_manager&pmod=search&succeed=1', 'succeed');
		//echo $lang['groups_setting_succeed'];
echo <<<HTML
		<script type='text/javascript'>window.location='admin.php?action=plugins&identifier=group_manager&pmod=search&succeed=1';</script>
HTML;
	}
}

function serialize_para($key, $values){
	$ptn = '';
	foreach($values as $v){
		$ptn.=$key.'='.$v.'&';
	}
	return $ptn;
}
function searchmembers($condition, $limit=9999999, $start=0) {
	include_once libfile('class/membersearch');
	$ms = new membersearch();
	return $ms->search($condition, $limit, $start);
}
?>